package com.earnings.okhttputils.utils.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.earnings.okhttputils.utils.ui.dialog.ProgressDialog;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by Administrator on 2017/3/1 0001.
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    //此Fragment依附的Activity
    protected Activity mActivity;
    //此Fragment依附的Context
    protected Context mContext;
    protected Intent mIntent;
    protected Bundle bundleData;
    //继承此类的当前View
    protected View mView;

    /**
     * 初始化
     */
    protected abstract void init();

    /**
     * 传你的laoyutId来
     *
     * @return
     */
    protected abstract int onCreateLayout();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(onCreateLayout(), container, false);
        bundleData=getActivity().getIntent().getExtras();
        if(bundleData==null)bundleData=new Bundle( );
        init();
        return mView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        mContext = getContext();
        mIntent = new Intent();

    }

    /**
     * 普通跳转页面
     *
     * @param
     */
    protected void skip(Class activity) {
        mIntent.setClass(getContext(), activity);
        mIntent.putExtras( bundleData );
        startActivity(mIntent);
    }

    /**
     * 回调的跳转页面
     *
     * @param
     * @param requestCode
     */
    protected void skipResult(Class activity, int requestCode) {
        mIntent.setClass(getContext(), activity);
        startActivityForResult(mIntent, requestCode);
    }

    @Override
    public void onClick(View v) {

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getName()); //统计页面，"MainScreen"为页面名称，可自定义
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
    }

    /*
    招财汇宝会用到的可无视
    */
    public int getIdFromXML(String name,View view){
        return view.getResources().getIdentifier(name,"id", getActivity().getPackageName());
    }
    public int getDrawFromXML(String name,View view){
        return view.getResources().getIdentifier(name,"mipmap", getActivity().getPackageName());
    }
    public int getStringFromXML(String name,View view){
        return view.getResources().getIdentifier(name,"string", getActivity().getPackageName());
    }

    protected void showToast(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }


    private String title;
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    ProgressDialog progressDialog;
    protected void showProgress(){
        if(progressDialog==null)progressDialog=new ProgressDialog( getActivity() );
        progressDialog.show();
    }
    protected void showProgress(String text){
        if(progressDialog==null)progressDialog=new ProgressDialog( getActivity() );
        progressDialog.setText( text );
        progressDialog.show();
    }
    protected void dismissProgress(){
        if(progressDialog==null)return;
        if (progressDialog.isShowing())
            progressDialog.dismiss();
    }
}
